﻿//设置某一控件的值,ctls 是一个数组,是根据name值获得的，一般情况下就一个,radio可能会有多个
function setValue(ctls, value, type)
{
    var ctl = ctls[0];

    switch (type)
    {
        case "checkbox":
            if (value == "")
            {
                ctl.checked = "";
            }
            else
            {
                ctl.checked = "checked";
            }
            break;
        case "radio":
            for (var j = 0; j < ctls.length; j++)
            {
                if (ctls[j].value == formData[i].value)
                {
                    ctls[j].checked = "checked";
                    break;
                }
            }
            break;
        default:
            ctl.value = value;
            break;
    }
}
//初始化基础设置
var basicSet = [];
function initSet()
{
    for (var i = 0; i < basicSet.length; i++)
    {
        var ctl = document.getElementsByName(basicSet[i].name)[0];

        if (basicSet[i].MaxLength != "")
        {
            ctl.maxlength = basicSet[i].MaxLength;
        }
        if (basicSet[i].Prompt != "")
        {
            ctl.title = basicSet[i].Prompt;
        }
        if (basicSet[i].DefaultValue != "")//只是在新建的时候默认值才起作用
        {

            if (fromType == "0")
            {
                setValue(document.getElementsByName(basicSet[i].name), basicSet[i].DefaultValue, basicSet[i].type);
            }
        }
        if (basicSet[i].OnClick != "")
        {
            var j = i;
            ctl.onclick = function()
            {
                eval(basicSet[j].OnClick);
            }
        }
        if (basicSet[i].Regular != "")
        {

            if (basicSet[i].Regular.substr(0, 1) != "^")
            {
                basicSet[i].Regular = "^" + basicSet[i].Regular;
            }
            if (basicSet[i].Regular.substr(basicSet[i].Regular.length - 1, 1) != "$")
            {
                basicSet[i].Regular += "$";
            }

            var errorMessage = "验证错误";
            if (basicSet[i].ErrorMessage != "")
            {
                errorMessage = basicSet[i].ErrorMessage;
            }

            //加上验请证事件
            AddEvent(ctl, "blur", basicSet[i].Regular, ctl, errorMessage, false);
            AddEvent(document.forms[0], "submit", basicSet[i].Regular, ctl, errorMessage, true);


        }
        if (basicSet[i].Require == "1")
        {
            var errorMessage = "请填写相关数据";
            if (basicSet[i].ErrorMessage != "")
            {
                errorMessage = basicSet[i].ErrorMessage;
            }
            AddEvent(ctl, "blur", "^\\s*\\S+\\s*$", ctl, errorMessage, false);
            AddEvent(document.forms[0], "submit", "^\\s*\\S+\\s*$", ctl, errorMessage, true);
        }

    }
}
var arrEvents = []; //验证事件的函数数组
function AddEvent(ctl, eventName, strRegex, targetCtl, errorMessage, isFocus)
{
    if (eventName == "submit")//应该在点击发送按钮时触发
    {
        arrEvents.push(function()
        {
            var regex = new RegExp(strRegex);
            if (!regex.test(targetCtl.value))
            {
                alert(errorMessage);
                if (isFocus)
                {
                    targetCtl.focus();
                }
                return false;
            }
            else
            {
                return true;
            }
        });
    }
    else
    {
        if (document.all)
        {
            ctl.attachEvent("on" + eventName, function()
            {
                var regex = new RegExp(strRegex);               
                if (!regex.test(targetCtl.value))
                {
                    alert(errorMessage);
                    if (isFocus)
                    {
                        targetCtl.focus();
                    }
                    return false;
                }
            });
        }
        else
        {
            ctl.addEventListener(eventName, function()
            {
                var regex = new RegExp(strRegex);
               
                if (!regex.test(targetCtl.value))
                {
                    
                    alert(errorMessage);
                    if (isFocus)
                    {
                        targetCtl.focus();
                    }
                    arguments[0].preventDefault(); //阻止事件执行，主提交事件                
                }
            }, false);
        }
    }
}



//初始化权限
var invisibleCtls = [];
var editCtls = [];
function initRight()
{
    for (var i = 0; i < invisibleCtls.length; i++)
    {
        document.forms[0][invisibleCtls[i]].style.display = "none";
    }

    for (var i = 0; i < editCtls.length; i++)
    {
        document.forms[0][editCtls[i]].readOnly = "";
        document.forms[0][editCtls[i]].disabled = "";

    }
}

//初始化表单数据。在非新建的情况下
var formData = [];
function initData()
{
   
    for (var i = 0; i < formData.length; i++)
    {
        var ctls = document.getElementsByName(formData[i].name);
        var ctl = ctls[0];
      

        if (fromType != "0" && addRight)//如果没不新建则加上权限控制
        {
            ctl.readOnly = "readonly"; //默认情况下设为只读
            //ctl.disabled = "disabled";

            //查找一下有没有编辑权限
            var canEdit = false;
            for (var j = 0; j < editCtls.length; j++)
            {
                if (ctl.name == editCtls[j])
                {
                    canEdit = true;
                    break;
                }
            }
            if (!canEdit)
            {
                ctl.onclick = null;
            }
        }


        setValue(ctls, formData[i].value, formData[i].type);
    }
}

function showForm(url)//弹出发送对话框，弹出之前要进行验证
{
    var flag = true;
    for (var i = 0; i < arrEvents.length; i++)
    {
        flag = arrEvents[i]();
        if (flag == false)
        {
            break;
        }
    }
    if (flag)
    {
        //把表单数据发送到服务器存session里

        var para = {};
      
        $(":text,:radio[checked=true],:checkbox[checked=true],select,textarea").each(function(i)
        {
            para[$(this).attr("name")] = $(this).val();

        });

      
        $.post("SaveToSession.aspx", para, function(result)
        {
           
            CreateDivFrm("提交表单", null, url, 350, 300);
        });        
        
    }
}